#-*-coding:utf-8-*-#

import os
import pandas as pd
import matplotlib.pyplot as plt

os.chdir('PATH')

dr = pd.read_csv('Data_Extended4.csv') # Drought Twitter data with sentiment analysis
wf = pd.read_csv('Data_Fig1c_1.csv') # Wildfire Twitter data with sentiment analysis
ref = pd.read_csv('Data_Fig1c_3.csv') # dates of 2012 March-December


dr['Date'] = pd.to_datetime(dr['Date'], errors='coerce')
dr['Date'] = dr['Date'].dt.strftime("%Y-%m-%d")
dates = dr['Date'].drop_duplicates()
dates= dates.sort_values()
dates= dates.reset_index(drop=True)
dates = dates.dropna()

D1 = dr[~dr['Retweet_to'].isna()]
D2 = dr[~dr['Reply_to'].isna()]
D = pd.concat([D1,D2])

wf['Date'] = pd.to_datetime(wf['Date'], errors='coerce')
wf['Date'] = wf['Date'].dt.strftime("%Y-%m-%d")
dates = wf['Date'].drop_duplicates()
dates= dates.sort_values()
dates= dates.reset_index(drop=True)
dates = dates.dropna()

W1 = wf[~wf['Retweet_to'].isna()]
W2 = wf[~wf['Reply_to'].isna()]
W = pd.concat([W1,W2])

dr_date = pd.DataFrame()
dr_date['Date'] = dates
dr_date['positive'] = 0
dr_date['negative'] = 0
dr_date['neutral'] = 0

wf_date = pd.DataFrame()
wf_date['Date'] = dates
wf_date['positive'] = 0
wf_date['negative'] = 0
wf_date['neutral'] = 0

for i in range(len(dates)):
    total = D[D['Date']== dates[i]]
    polar = total.groupby(['polarity']).count()
    for j in range(len(polar)):
        dr_date[polar.index[j]].iloc[i]= polar['Date'][polar.index[j]]

for i in range(len(dates)):
    total = W[W['Date']== dates[i]]
    polar = total.groupby(['polarity']).count()
    for j in range(len(polar)):
        wf_date[polar.index[j]].iloc[i]= polar['Date'][polar.index[j]]

plt.figure(dpi=400,figsize=(6,2))

plt.plot(dr_date['Date'],dr_date['negative'], color='red',linestyle='-',label='Negative')
plt.plot(dr_date['Date'],dr_date['positive'], color='blue',linestyle='-',label='Positive')
plt.xticks(range(0,len(dr_date),60))
plt.ylabel('Twitter')
plt.xlim([0,len(dr_date)-1])
plt.ylabel('Twitter (x100)')
plt.yticks(ticks=[0,250,500,750,1000], labels=['0','2.5','5','7.5','10'])
plt.legend()
plt.tight_layout()
plt.show()

plt.figure(dpi=400,figsize=(6,2))

plt.plot(wf_date['Date'],wf_date['negative'], color='red',linestyle='-',label='Negative')
plt.plot(wf_date['Date'],wf_date['positive'], color='blue',linestyle='-',label='Positive')
plt.xticks(range(0,len(wf_date),60))
plt.ylabel('Twitter')
plt.xlim([0,len(wf_date)-1])
plt.ylabel('Twitter (x100)')
plt.yticks(ticks=[0,50,100,150,200,250,300], labels=['0','0.5','1','1.5','2','2.5','3'])
plt.legend()
plt.tight_layout()
plt.show()

